import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-01-13
 * Time: 12:07
 */
public class Test {
    // 二维数组的打印
    public static void main(String[] args) {
        int[][] array = {{1,2,3,4},{5,6,7,8}};
        for (int i = 0; i < array.length; i++) {    // 方法一：采用for循环
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("==============");
        for (int[] ret:array) {       // 方法二：采用foreach循环
            for (int x:ret) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        System.out.println("==============");
        System.out.println(Arrays.deepToString(array)); // 方法三：直接采用Arrays中的deepToString方法
    }
    // 二维数组的几种创建方法
    public static void main3(String[] args) {
        int[][] array = {{1,2,3,4},{5,6,7,8}};
        int[][] array2 = new int[][]{{1,2,3,4},{5,6,7,8}};
        int[][] array3 = new int[2][3];
    }
    // 将数组逆置
    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int temp = array[left];
            array[left] = array[right];
            array[right] = temp;
            left++;
            right--;
        }
    }

    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};
        reverse(array);
        System.out.println(Arrays.toString(array));

    }
    // 冒泡排序
    public static void bubblesort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {   // 总共要走array.length-1趟
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) { // 每趟要比较的次数为array.length-1-i次
                if (array[j] > array[j + 1]) {  // 如果这个数大于要比较的数则交换数据
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    flg = true;
                }
            }
            if (flg == false) {      // 如果有其中一趟没有交换，则说明数组已经有序了
                break;
            }
        }
    }
    public static void main1(String[] args) {
        int[] array = {1,21,3,14,5};
        bubblesort(array);
        System.out.println(Arrays.toString(array));
    }
}
